package com.tanhua.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.common.utils.NoAuthorization;
import com.tanhua.server.service.TestSoulService;
import com.tanhua.server.vo.AnswersVo;
import com.tanhua.server.vo.QuestionnaireVo;
import com.tanhua.server.vo.TestSoulReportVo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.server.controller
 * @Description:
 * @date 2021/9/14 13:55
 */
@RequestMapping("testSoul")
@RestController
public class TestSoulController {

    @Autowired
    private TestSoulService soulService;

    @GetMapping()
    public ResponseEntity<List<QuestionnaireVo>> queryQuestionnaireList() {
        try {
            List<QuestionnaireVo> list = this.soulService.queryQuestionnaireList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @PostMapping
    @NoAuthorization
    public ResponseEntity<String> postAnswerList(@RequestBody Map<String, List<AnswersVo>> answers) {
        try {
//            System.out.println(answers);
            List<AnswersVo> list = answers.get("answers");
            if (!ObjectUtil.isEmpty(list)) {
                String reportId = this.soulService.postAnswerList(list);
                return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    @GetMapping("report/{id}")
    public ResponseEntity<TestSoulReportVo> queryReport(@PathVariable Integer id) {
        try {
            TestSoulReportVo report = this.soulService.queryReport(id);
            return ResponseEntity.ok(report);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

}
